/**
 * 时间戳格式化工具
 * @param {时间戳} timestamp
 * @param {格式} style
 */
export const parseTime = (
  timestamp: string | number,
  style = 'YY-MM-DD HH:mm:ss 星期W',
  add0Style = [] // ['MM','DD','HH','mm','ss']
): string => {
  // 为了兼容ios需要将-替换成/
  // 首先判定style的格式
  if (!timestamp) {
    return '-';
  }
  const _timestamp =
    typeof timestamp === 'string'
      ? String(timestamp).replace(/-/g, '/')
      : timestamp;
  const date = new Date(_timestamp);
  const weekTxt = ['日', '一', '二', '三', '四', '五', '六'];
  let dateData = {
    YY: date.getFullYear(),
    MM: date.getMonth() + 1,
    DD: date.getDate(),
    HH: date.getHours(),
    mm: date.getMinutes(),
    ss: date.getSeconds(),
    W: weekTxt[date.getDay()],
  };
  for (const key in dateData) {
    if (Object.prototype.hasOwnProperty.call(dateData, key)) {
      let value = dateData[key].toString();
      if (add0Style.includes(key)) {
        value = value.padStart(2, '0');
      }
      style = style.replace(key, value);
    }
  }
  return style;
};
/**
 * 拆解时间字符串
 * @param date
 * @returns
 */
export const splitDateString = (date: string) => {
  const value = date.replace(/-|:/g, ' ').split(' ');
  const type = ['YY', 'MM', 'DD', 'HH', 'mm', 'ss', 'W'];
  return type.reduce((prev, next, index) => {
    prev[next] = value[index];
    return prev;
  }, {});
};

// console.log(parseTime(new Date().getTime(), 'YY-MM-DD-HH:mm:ss'));
